import { ArgumentMetadata, HttpException, HttpStatus, Injectable, PipeTransform } from '@nestjs/common';
import { plainToInstance } from 'class-transformer';
import { validate } from 'class-validator';

@Injectable()
export class PPipe implements PipeTransform {
  async transform(value: any, metadata: ArgumentMetadata) {
    console.log(value, metadata);
    const DTO = plainToInstance(metadata.metatype!, value)
    console.log(DTO);

    const errors = await validate(DTO)

    if(errors.length > 0) {
      throw new HttpException(errors, HttpStatus.BAD_REQUEST)
    }

    console.log(errors);
    
    return value;
  }
}
